function R = select(lst, pred, op)
%SELECT Selects a subset of elements from the list satisfying some
%condition
%
% [ Syntax ]
%   - slst = select(lst, pred)
%   - C = select(lst, pred, 'cell')
%
% [ Description ]
%   - slst = select(lst, pred) selects a sub-list of elements for which
%     pred(x) is true.
%   
%   - slst = select(lst, pred, 'cell') returns the selected list in form
%     of a cell array.
%
% [ History ]
%   - Created by Dahua Lin, on Jan 5, 2007
%

%% parse and verify input

if nargin < 3
    make_list = true;
else
    if ischar(op) && strcmp(op, 'cell')
        make_list = false;
    else
        error('sltoolbox:sllist:select:invalidarg', ...
            'The 3rd argument is invalid');
    end
end

%% main

C = cell(lst);

p = cellfun(pred, C);
if ~islogical(p)
    p = logical(p);
end

R = C(p);
if make_list
    R = sllist(R);
end
    